//
//  StatusCell.swift
//  weibo
//
//  Created by 吴婷婷 on 15/11/10.
//  Copyright © 2015年 wutingting. All rights reserved.
//

import UIKit
import SnapKit

/// 微博 Cell 中控件的间距数值
let StatusCellMargin: CGFloat = 12
/// 微博头像的宽度
let StatusCellIconWidth: CGFloat = 35

class StatusCell: UITableViewCell {
    ///微博视图模型
    var viewModel: StatusVewModel? {
        didSet{
            
            topView.viewModel = viewModel
            pictureView.viewModel = viewModel
            
            contentLabel.text = viewModel?.status.text
            //测试修改配图视图行高
            pictureView.snp_updateConstraints { (make) -> Void in

                make.height.equalTo(pictureView.bounds.height)
                make.width.equalTo(pictureView.bounds.width)
            }
        }
    }
    ///根据视图模型计算行高
    func rowHeight(vm: StatusVewModel) -> CGFloat {
        viewModel = vm
        
        contentView.layoutIfNeeded()
        
        return CGRectGetMaxY(bottomView.frame)
    }
    
    ///构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .Subtitle, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
        selectionStyle = .None
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 懒加载控件
    private lazy var topView: StatusCellTopView = StatusCellTopView()
    
    lazy var contentLabel: UILabel = UILabel(title: "微博正文",  fontSize: 15, screenInsert: 2 * StatusCellMargin)
    
    lazy var pictureView: StatusCellPictureView = StatusCellPictureView()
    
    lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
    
    
}

//MARK: - 设置界面
extension StatusCell {
    
    func setupUI() {
        //1.添加控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
//        contentLabel.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * StatusCellMargin
        
        // 2. 设置约束
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(2 * StatusCellMargin + StatusCellIconWidth)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left).offset(StatusCellMargin)
//            make.bottom.equalTo(contentView.snp_bottom).offset(-StatusCellMargin)
        }
        
        
        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(pictureView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
            //指定向下约束
//            make.bottom.equalTo(contentView.snp_bottom)
        }
    }
    
}
